var DOMParser = require('xmldom').DOMParser;
/**
 * 用户消息
 */
function WechatMsg(msg){
	//TODO: xmldom 的使用细节需要推敲一下
	var doc = new DOMParser().parseFromString(msg)
	  , toUserName = doc.documentElement.getElementsByTagName('ToUserName')
	  , fromUserName = doc.documentElement.getElementsByTagName('FromUserName')
	  , createTime = doc.documentElement.getElementsByTagName('CreateTime')
	  , msgType = doc.documentElement.getElementsByTagName('MsgType')
	  , content = doc.documentElement.getElementsByTagName('Content')
	  , picUrl = doc.documentElement.getElementsByTagName('PicUrl')
	  , locationX = doc.documentElement.getElementsByTagName('Location_X')
	  , locationY = doc.documentElement.getElementsByTagName('Location_Y')
	  , label = doc.documentElement.getElementsByTagName('Label')
	  , event = doc.documentElement.getElementsByTagName('Event')
	  , eventKey = doc.documentElement.getElementsByTagName('EventKey')
	  , mediaId = doc.documentElement.getElementsByTagName('MediaId')
	  , format = doc.documentElement.getElementsByTagName('Format')
	  , recognition = doc.documentElement.getElementsByTagName('Recognition')
      , ticket = doc.documentElement.getElementsByTagName('Ticket');
	
	this.toUserName = toUserName[0].firstChild.data;
	this.fromUserName = fromUserName[0].firstChild.data;
	this.createTime = createTime[0].firstChild.data;
	this.msgType = msgType[0].firstChild.data;
	if(content[0]){
		this.content = content[0].firstChild.data;
	}
	if(picUrl[0]){
		this.picUrl = picUrl[0].firstChild.data;
	}
	if(locationX[0]){
		this.locationX = locationX[0].firstChild.data;
	}
	if(locationY[0]){
		this.locationY = locationY[0].firstChild.data;
	}
	if(label[0] && label[0].firstChild != null){
		this.label = label[0].firstChild.data;
	}
	if(event[0] && event[0].firstChild != null){
		this.event = event[0].firstChild.data;
	}
	if(eventKey[0] && eventKey[0].firstChild != null){
		this.eventKey = eventKey[0].firstChild.data;
	}
	if(mediaId[0]){
		this.mediaId = mediaId[0].firstChild.data;
	}
	if(format[0]){
		this.format = format[0].firstChild.data;
	}
	if(recognition[0] && recognition[0].firstChild != null){
		this.recognition = recognition[0].firstChild.data;
	}
    if(ticket[0]){
        this.ticket = ticket[0].firstChild.data;
    }
}

module.exports = WechatMsg;


